%%
%第二问的代码
chang = 3.41-2* 0.275;
banjing = 16*0.55;
jiaodu = 1/banjing;
x_tou = zeros(2,301);
qiyu_zuobiao1 = zeros(224,301);
qiyu_zuobiao2 = zeros(224,301);
jiaodu_quanbu = zeros(301,1);

qiyu_zuobiao_dier1 = zeros(901,224);
qiyu_zuobiao2_dier2 = zeros(901,224);
jiaodu_quanbu_dier = zeros(900,1);
for i = 0:900
    jiaodu1_huchang = 16*2*pi-jiaodu*i;
    jiaodu_quanbu_dier(i+1,1) = jiaodu1_huchang; 
    x_tou(i+1,1) = 0.55*jiaodu1_huchang/(2*pi)*cos(jiaodu1_huchang);
    x_tou(i+1,2) = 0.55*jiaodu1_huchang/(2*pi)*sin(jiaodu1_huchang);
    leiji_theta = jiaodu1_huchang;
    bianjingwenti = 0.55*jiaodu1_huchang/(2*pi);
  for j = 2:223
    if j == 2
      tou_qiyu =2.86; 
    else
      tou_qiyu = 1.65; 
    end
    %角度情况
      bianhua_heta = tou_qiyu / bianjingwenti;
      leiji_theta = leiji_theta + bianhua_heta;
      bianjingwenti = 0.55*leiji_theta/(2 * pi);
    %每一个的位置
     qiyu_zuobiao_dier1(i+1, j) = bianjingwenti * cos(leiji_theta);
     qiyu_zuobiao2_dier2(i+1, j) = bianjingwenti * sin(leiji_theta);
  end
end

for t =1:500
    %每一个的具体的值
    for a =1:223-1
        for b =2:223-1     
            P1 = [qiyu_zuobiao_dier1(t,a), qiyu_zuobiao2_dier2(t,a)];
            P2 = [qiyu_zuobiao_dier1(t,a+1), qiyu_zuobiao2_dier2(t,a+1)];           
            Q1 = [qiyu_zuobiao_dier1(t,b), qiyu_zuobiao2_dier2(t,b)];
            Q2 = [qiyu_zuobiao_dier1(t,b), qiyu_zuobiao2_dier2(t,b)];            
            % 判断是否相交
            jitiqingkuang = shifouxiangjiao(P1, P2, Q1, Q2);
            if jitiqingkuang
                 disp(['时间',num2str(zuo)]);
            end
        end
    end
end

function [jitiqingkuang] = shifouxiangjiao(P1, P2, Q1, Q2) 
    jitiqingkuang1Q1 = direction(P2, P1, Q1) * direction(P2, P1, Q2) < 0;
    jitiqingkuang1Q2 = direction(P2, Q1, Q2) * direction(P2, Q1, P1) < 0;
    jitiqingkuangP1 = direction(Q2, Q1, P1) * direction(Q2, Q1, P2) < 0;
    jitiqingkuangP2 = direction(Q2, P1, P2) * direction(Q2, P1, Q1) < 0;
    jitiqingkuang = (jitiqingkuang1Q1 && jitiqingkuang1Q2) || (jitiqingkuangP1 && jitiqingkuangP2);
end
function d = direction(p1, p2, q)
      d = (p2(1)- p1(1))*(q(1) -p1(2))-(p2(2)-p1(2))*(q(2)-p1(2));
end   
